package com.dmj.sqldsl.executor.visitor;

import com.dmj.sqldsl.executor.exception.NoOrderByException;
import com.dmj.sqldsl.model.DslQuery;
import com.dmj.sqldsl.model.Limit;

public class SqlserverQueryModelVisitor extends QueryModelVisitor {

  private boolean noOrderBy;

  @Override
  public String visit(DslQuery query) {
    noOrderBy = !query.getOrderBy().isPresent();
    return super.visit(query);
  }

  /**
   * 只兼容sqlserver 2012版本以上
   */
  @Override
  protected String visit(Limit limit) {
    if (noOrderBy) {
      throw new NoOrderByException("Sql server need order statement when call limit!");
    }
    params.add(new Parameter(limit.getOffset() * limit.getSize()));
    params.add(new Parameter(limit.getSize()));
    return " offset ? rows fetch next ? rows only";
  }
}
